package com.gexin.controller.system.agent;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.system.AddrAreas;
import com.gexin.model.system.AddrCities;
import com.gexin.service.agent.AgentService;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * 代理商区域管理
 */
@ControllerBind(controllerKey = "/system/agent/area", viewPath = "WEB-INF/system/agent/")
public class AgentAreaController extends SystemBaseController {

	/**
	 * 获取所有的区域,最小粒度为市一级
	 */
	public void index(){
		if(isAjax()){
			String name = getPara("name");
			Page<AddrCities> pageArea = AgentService.dao.findPageArea(getpage() , getpageSize(),name);
			renderJson(pageArea);
			return;
		}
		render("area_list.html");
	}

	/**
	 * 修改区域的价格信息
	 */
	public void updateFee(){
		AddrCities city = getModel(AddrCities.class , "city");
		ApiResponse apiResponse = AgentService.dao.updateCityAreaFee(city);
		renderJson(apiResponse);

	}

	/**
	 * 根据城市id查询所有的区域
	 * /system/agent/area/areaList
	 */
	public void areaList(){
		String cityId = getPara("city_id");
		if(StringUtil.isBlank(cityId)){
			fail("城市id不能为null!");
			return;
		}
		AddrCities city = AddrCities.dao.findByCityId(cityId);
		if(city.getIsArea()==null||city.getIsArea()==0){
			fail("该城市未开启下级代理!");
			return;
		}
		List<AddrAreas> areas = AddrAreas.dao.getAreasByCityId(cityId);
		success(areas);
	}

	/**
	 * 是否开启下级代理
	 */
	public void openArea(){
		String id = getPara("id");
		Integer state = getParaToInt("state");
		AddrCities city = AddrCities.dao.findById(id);
		city.setIsArea(state);
		boolean update = city.update();
		renderJson(new ApiResponse(update,update?"修改成功!":"修改失败!"));
	}

	/**
	 * 更新地区价格
	 */
	public void updateAreaFee(){
		AddrAreas area = getModel(AddrAreas.class , "area");
		ApiResponse apiResponse = AgentService.dao.updateAreaFee(area);
		renderJson(apiResponse);
	}



}
